package com.itheima.web.manager;

import cn.hutool.core.date.DateUtil;
import com.itheima.service.db.AnalysisByDayService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AnalysisManager {

    @Reference
    AnalysisByDayService analysisByDayService;

    public ResponseEntity findSummary() {
        return ResponseEntity.ok(analysisByDayService.findSummary());
    }

    public ResponseEntity findData(Long start, Long end, String type) {

        //今年
        String startDay = DateUtil.format(DateUtil.date(start),"yyyy-MM-dd");
        String endDay = DateUtil.format(DateUtil.date(end),"yyyy-MM-dd");
        //去年
        String lastStartDay = DateUtil.format(DateUtil.offsetDay(DateUtil.date(start), -365), "yyyy-MM-dd");
        String lastEndDay = DateUtil.format(DateUtil.offsetDay(DateUtil.date(end), -365), "yyyy-MM-dd");

        //判断操作类型
        if (type.equals("101")){
            return ResponseEntity.ok(analysisByDayService.findNewUserData(startDay, endDay,lastStartDay,lastEndDay));
        }else if (type.equals("102")){
            return ResponseEntity.ok(analysisByDayService.findActiveData(startDay, endDay,lastStartDay,lastEndDay));
        }else if (type.equals("103")){
            return ResponseEntity.ok(analysisByDayService.findNumRetention(startDay, endDay,lastStartDay,lastEndDay));
        }else {
            return null;
        }
    }
}
